#!/usr/bin/bash

# 判断是否为root用户
if [ `whoami` != "root" ];then
    echo " only root can run it"
    exit 1
fi

# 判断能否访问外网
ping -c 2 www.baidu.com
if [ $? != 0  ];then
    echo "Please connect to the Internet!"
    exit 2
fi

# 配置阿里云yum源
yum -y install wget
mkdir /tmp/yum_repos_backup
mv /etc/yum.repos.d/* /tmp/yum_repos_backup/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache

# 关闭防火墙
systemctl stop firewalld.service &> /dev/null
systemctl disable firewalld.service &> /dev/null

# 关闭selinux
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config

# 安装常用工具
yum -y install vim curl bash-completion unzip tree dos2unix lvm2 bind-utils net-tools
yum -y install gcc gcc-c++ make cmake autoconf openssl-devel openssl-perl net-tools psmisc rsync sudo lrzsz
source /usr/share/bash-completion/bash_completion

# 设置最大打开文件描述符数
echo "ulimit -SHn 65535" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
cat >> /etc/security/limits.conf <<-EOF
* soft nofile 65535
* hard nofile 65535
EOF

# 升级最新内核
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# yum --enablerepo=elrepo-kernel install -y kernel-ml
# grub2-set-default 0
# grub2-mkconfig -o /boot/grub2/grub.cfg

# 关闭ssh的反向DNS解析
sed -i '/^#UseDNS/s/#//' /etc/ssh/sshd_config
sed -i '/^UseDNS/s/yes/no/' /etc/ssh/sshd_config
systemctl restart sshd

# 配置vim缩进
echo "set tabstop=4" >> /etc/vimrc
echo "set softtabstop=4" >> /etc/vimrc
echo "set shiftwidth=4" >> /etc/vimrc

# 关闭铃声
sed -i '2s/#//' /etc/inputrc

# 精简开机启动程序
systemctl list-unit-files | grep enabled | \
  egrep -v "sshd.service|crond.service|sysstat|rsyslog|^NetworkManager.service|irqbalance.service"   | awk '{print "systemctl disable", $1}' | bash

# 设置语言为中文
#localectl list-locales | grep z
#LANG=zh_CN.utf8
#localectl set-locale LANG=zh_CN.utf8

# 设置主机名
read -p "请输入您需要设置的主机名：" host_name
echo "$host_name" > /etc/hostname